package com.informationsystem.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.informationsystem.service.NewsManager;
import com.informationsystem.service.PictureNewsManager;
import com.informationsystem.dao.SearchException;
import com.informationsystem.model.News;
import com.informationsystem.model.PictureNews;
import com.informationsystem.webapp.action.BaseAction;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import net.coobird.thumbnailator.Thumbnails;

import org.apache.struts2.ServletActionContext;

public class PictureNewsAction extends BaseAction implements Preparable {
    private PictureNewsManager pictureNewsManager;
    private NewsManager newsManager;
    private List pictureNewss;
    private PictureNews pictureNews;
    private Long id;
    private String query;
    private File picture;
    private String contentType;
    private String filename;
    private String viewPictureName;

    public void setPictureNewsManager(PictureNewsManager pictureNewsManager) {
        this.pictureNewsManager = pictureNewsManager;
    }

    public List getPictureNewss() {
        return pictureNewss;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String pictureNewsId = getRequest().getParameter("pictureNews.id");
            if (pictureNewsId != null && !pictureNewsId.equals("")) {
                pictureNews = pictureNewsManager.get(new Long(pictureNewsId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        try {
            pictureNewss = pictureNewsManager.search(query, PictureNews.class);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            pictureNewss = pictureNewsManager.getAll();
        }
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public PictureNews getPictureNews() {
        return pictureNews;
    }

    public void setPictureNews(PictureNews pictureNews) {
        this.pictureNews = pictureNews;
    }

    public String delete() {
    	pictureNews = pictureNewsManager.get(pictureNews.getId());
    	pictureNews.setNews(null);
    	File file = new File(ServletActionContext.getServletContext().getRealPath("/picturenews")+"\\"+
    										pictureNews.getPictureName());
    	file.delete();
        pictureNewsManager.remove(pictureNews);
        saveMessage(getText("pictureNews.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            pictureNews = pictureNewsManager.get(id);
        } else {
            pictureNews = new PictureNews();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (pictureNews.getId() == null);
        
        pictureNews.setNews(newsManager.get(pictureNews.getNews().getId()));
        if(isNew)
        	pictureNews.setPictureName(filename);
        else{
        	rename(pictureNews.getPictureName(),viewPictureName);
        	pictureNews.setPictureName(viewPictureName);
        }
        
        pictureNews = pictureNewsManager.save(pictureNews);
        if(isNew)
        	saveFile(filename,picture);
        String key = (isNew) ? "pictureNews.added" : "pictureNews.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
    
    public void rename(String oldfilename, String newfilename){
    	String path = ServletActionContext.getServletContext().getRealPath("/picturenews")+"\\";
		File oldfile =new File(path+ oldfilename);
		File newfile =new File(path+ newfilename);
 
		oldfile.renameTo(newfile);
    }

    public void saveFile(String tosavefilename, File tosavefile) throws IOException{
		String path = ServletActionContext.getServletContext().getRealPath("/picturenews");
        Thumbnails.of(tosavefile.getPath())     
        .size(327, 219)     
        .keepAspectRatio(false)     
        .toFile(path+"\\"+tosavefilename);
    }
    
	public NewsManager getNewsManager() {
		return newsManager;
	}

	public void setNewsManager(NewsManager newsManager) {
		this.newsManager = newsManager;
	}
	
	public List getNewsList(){
		return newsManager.getAll();
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}
	
    public void setPictureContentType(String contentType) {
        this.contentType = contentType;
     }

     public void setPictureFileName(String filename) {
        this.filename = filename;
     }
     
     public String getViewPictureName(){
    	 return pictureNews.getPictureName();
     }
     
     public String setViewPictureName(String viewPictureName){
    	 return this.viewPictureName = viewPictureName;
     }
}